package com.android.fastride.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.android.fastride.FastRideApplication;
import com.android.fastride.activity.base.BaseActivity;
import com.android.fastride.manager.UserManager;
import com.android.fastride.model.http.entity.LoginUserInfoEntity;
import com.android.fastride.model.repository.ControlSystemRepository;
import com.android.fastride.model.sharedpreference.SPreferBase;
import com.peng.android.coremodule.utils.RxSchedulerUtils;

import rx.Subscriber;

public class WelcomeActivity extends BaseActivity {
    private static final String TAG = "WelcomeActivity";

    Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!isTaskRoot()){
            Intent i=getIntent();
            String action=i.getAction();
            if(i.hasCategory(Intent.CATEGORY_APP_CALENDAR)
                    && !TextUtils.isEmpty(action)
                    && action.equals(Intent.ACTION_MAIN)){
                finish();
                return;
            }
        }
        requestPermission();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        requestPermission();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        Log.i(TAG, "onPermissionSuccess: 请求权限成功！");
        if (TextUtils.isEmpty(SPreferBase.getInstance().getString(SPreferBase.BENGUO_CONF_DEVID, null))) {
//            FastRideApplication.getApp().initPushService();
        }
        logIn();
    }

    @Override
    protected void logIn() {
        super.logIn();

        LoginUserInfoEntity loginUserInfoEntity = UserManager.getInstance().getUserInfo();

        ControlSystemRepository.<String>loginByPwd(loginUserInfoEntity.getPhone(),
                loginUserInfoEntity.getPassword(), "")
                .compose(RxSchedulerUtils.<LoginUserInfoEntity>normalSchedulersTransformer())
//                .compose(this.<LoginUserInfoEntity>bindUntilEvent(PresentEvent.DESTROY))
                .subscribe(new Subscriber<LoginUserInfoEntity>() {
                    @Override
                    public void onCompleted () {
                        Log.i(TAG, "登录成功");
                    }

                    @Override
                    public void onError (Throwable e) {
                        Log.i(TAG, "onError: 自动登录失败");
                        e.printStackTrace();
                        //清空用户数据，并且进去登录页面
                        FastRideApplication.getApp().clearUserInfo();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run () {
                                LoginActivity.start(WelcomeActivity.this);
                                finish();
                            }
                        }, 1000);
                    }

                    @Override
                    public void onNext (LoginUserInfoEntity s) {
                        Log.i(TAG, "onNext: 自动登录成功！");
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run () {
                                MainActivity.start(WelcomeActivity.this);
                                finish();
                            }
                        }, 1000);
                    }
                });
    }
}
